package com.proposition.web.paper;

import com.proposition.commons.constant.ErrorCode;
import com.proposition.commons.constant.PaperPermission;
import com.proposition.commons.enums.CheckStatus;
import com.proposition.paper.service.PaperService;
import com.proposition.support.annotation.LogAnnotation;
import com.proposition.support.utils.Rest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 试卷审核控制器
 *
 * @author 杨凯
 * @create 2018-01-31 14:53
 **/
@Slf4j
@Controller
@RequestMapping("/paper")
public class PaperCheckController {

	@Autowired
	private PaperService paperService;

	/**
	 * 试卷审核
	 *
	 * @param paperId     试卷Id
	 * @param checkStatus 试卷审核状态
	 * @param mark        备注
	 * @param flag        是否通过审核
	 * @return
	 */
	@LogAnnotation(moduleName = "试卷模块", operate = "试卷审核")
	@ResponseBody
	@PostMapping(value = "/check", produces = "application/json")
	public Rest checkPaper(@RequestParam String paperId, @RequestParam String checkStatus, String mark, @RequestParam boolean flag) {
		Rest.Builder rest = Rest.newBuilder();
		try {
			CheckStatus status = CheckStatus.stringFormatEnum(checkStatus);
			//权限控制
			Subject currentUser = SecurityUtils.getSubject();
			switch (status) {
				case WEI_SHEN_HE:
				case YI_SHEN_UNPASS:
					if (!currentUser.isPermitted(PaperPermission.CHECK_ONE_PAPER)) {
						return rest.status(ErrorCode.PERMISSION_DENIED).build();
					}
					break;
				case YI_SHEN_PASS:
				case ER_SHEN_UNPASS:
					if (!currentUser.isPermitted(PaperPermission.CHECK_TWO_PAPER)) {
						return rest.status(ErrorCode.PERMISSION_DENIED).build();
					}
					break;
				case ER_SHEN_PASS:
				case SAN_SHEN_UNPASS:
					if (!currentUser.isPermitted(PaperPermission.CHECK_THREE_PAPER)) {
						return rest.status(ErrorCode.PERMISSION_DENIED).build();
					}
					break;
				default:
					log.error("试题审核状态出现错误：[{}]", status);
					return rest.build();
			}
			paperService.checkPaper(paperId, checkStatus, mark, flag);
		} catch (Exception e) {
			log.error("试卷审核失败[{}]", e.getMessage());
			return rest.status(ErrorCode.UNEXPECTED_ERROR).build();
		}
		return rest.status(ErrorCode.SUCCESS).build();
	}

	/**
	 * 试卷审核状态撤销
	 *
	 * @param paperId     试卷Id
	 * @param checkStatus 试卷审核状态
	 * @return
	 */
	@LogAnnotation(moduleName = "试卷模块", operate = "撤销审核")
	@ResponseBody
	@PostMapping(value = "/revoke", produces = "application/json")
	public Rest revoke(@RequestParam String paperId, @RequestParam String checkStatus) {
		Rest.Builder rest = Rest.newBuilder();
		try {
			CheckStatus status = CheckStatus.stringFormatEnum(checkStatus);
			//权限控制
			Subject currentUser = SecurityUtils.getSubject();
			switch (status) {
				case YI_SHEN_UNPASS:
				case YI_SHEN_PASS:
					if (!currentUser.isPermitted(PaperPermission.CHECK_ONE_PAPER)) {
						return rest.status(ErrorCode.PERMISSION_DENIED).build();
					}
					break;
				case ER_SHEN_PASS:
				case ER_SHEN_UNPASS:
					if (!currentUser.isPermitted(PaperPermission.CHECK_TWO_PAPER)) {
						return rest.status(ErrorCode.PERMISSION_DENIED).build();
					}
					break;
				case SAN_SHEN_PASS:
				case SAN_SHEN_UNPASS:
					if (!currentUser.isPermitted(PaperPermission.CHECK_THREE_PAPER)) {
						return rest.status(ErrorCode.PERMISSION_DENIED).build();
					}
					break;
				default:
					log.error("试题审核状态出现错误：[{}]", status);
					return rest.build();
			}
			paperService.revokePaper(paperId, checkStatus);
		} catch (Exception e) {
			log.error("撤销审核失败[{}]", e.getMessage());
			return rest.status(ErrorCode.UNEXPECTED_ERROR).build();
		}
		return rest.status(ErrorCode.SUCCESS).build();
	}


}
